package nc.hw;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Scanner;

public class HJ98 {

    public static class Good implements Comparable<Good>{
        String name;
        int price;
        int number;

        public Good(String name, int price, int number) {
            this.name = name;
            this.price = price;
            this.number = number;
        }

        @Override
        public int compareTo(Good o) {
            if (o.number == this.number) {
                return this.name.compareTo(o.name);
            }
            return this.number - o.number;
        }

        @Override
        public String toString() {
            return name +" " +price+" "+number;
        }
    }

    public static class StoreSystem {

        private int[] nums = new int[6];
        private int[] moneys = new int[4];

        private int[] moneyPrice = new int[]{
                1,2,5,10
        };

        private List<Good> goods = new ArrayList<>();


        private int totalNum = 0;
        private int userMoney = 0;

        public StoreSystem() {
            goods.add(new Good("A1",2,0));
            goods.add(new Good("A2",3,0));
            goods.add(new Good("A3",4,0));
            goods.add(new Good("A4",5,0));
            goods.add(new Good("A5",8,0));
            goods.add(new Good("A6",6,0));
        }

        public void init(String initCommand) {
            final String[] s = initCommand.split(" ");
            String[] nums = s[0].split("-");
            for (int i = 0; i < nums.length; i++) {
                this.goods.get(i).number = Integer.parseInt(nums[i]);
            }
            String[] moneys = s[1].split("-");
            for (int i = 0; i < moneys.length; i++) {
                this.moneys[i] = Integer.parseInt(moneys[i]);
                totalNum+=this.moneys[i];
            }
            System.out.println("S001:Initialization is successful");
        }

        public void p(int money) {
            // 检测money
            switch (money) {
                case 5:
                case 10:
                    if (moneys[0] + moneys[1] * 2 < money) {
                        System.out.println("E003:Change is not enough, pay fail");
                        return;
                    }
                case 1:
                case 2:
                    if (totalNum == 0) {
                        System.out.println("E005:All the goods sold out");
                        return;
                    }
                    break;
                default:
                    System.out.println("E002:Denomination error");
                    return;
            }
            userMoney += money;
            System.out.println("S002:Pay success,balance="+userMoney);
            switch (money){
                case 1:
                    moneys[0] ++;
                    break;
                case 2:
                    moneys[1] ++;
                    break;
                case 5:
                    moneys[2] ++;
                    break;
                case 10:
                    moneys[3] ++;
                    break;
                default:
                    break;
            }
        }

        public void b(String shop) {
            final Optional<Good> first = goods.stream().filter(good -> good.name.equals(shop)).findFirst();
            if (first.isPresent()) {
                final Good good = first.get();
                int price = good.price;
                if (good.number == 0) {
                    System.out.println("E007:The goods sold out");
                    return;
                }
                if (userMoney < price) {
                    System.out.println("E008:Lack of balance");
                    return;
                }
                userMoney -= price;
                System.out.println("S003:Buy success,balance="+userMoney);


            } else {
                System.out.println("E006:Goods does not exist");

            }
        }

        public void q(String i){
            switch (i) {
                case "0":
                    goods.stream().sorted()
                            .forEach(System.out::println);
                    break;
                case "1":
                    for (int i1 = 0; i1 < moneyPrice.length; i1++) {
                        System.out.println(moneyPrice[i1]+" yuan coin number="+moneys[i1]);
                    }
                    break;
                default:
                    System.out.println("E010:Parameter error");
            }
        }

        public void c() {
            if (userMoney == 0) {
                System.out.println("E009:Work failure");
            } else {
                // 退币
                int[] quitNumber = new int[4];
                for (int i = moneyPrice.length - 1; i >= 0; ) {
                    if (moneyPrice[i] > userMoney) {
                        i --;
                    } else {
                        quitNumber[i] ++;
                        userMoney -= moneyPrice[i];
                    }
                }
                for (int i = 0; i < quitNumber.length; i++) {
                    System.out.println(moneyPrice[i]+" yuan coin number="+quitNumber[i]);
                    moneys[i] -= quitNumber[i];
                }
                userMoney = 0;
            }
        }
    }

    public static void main(String[] args) {
//        Scanner scanner = new Scanner(System.in);
//        while (scanner.hasNext()) {
//
//        }

        final String s = "r 13-21-28-26-6-2 3-1-18-21;p 10;c;q0;p 1;b A6;q0;q1;c;c;c;q0;q1;b A3;q1;c;b A3;b A1;b A2;q1;q0;p 1;b A1;q0;q0;p 10;q1;q1;b A2;q0;q0;c;p 10;c;c;q0;p 1;b A4;p 2;c;b A5;c;p 10;b A5;q0;b A1;q0;b A2;q0;q1;";
        final String[] split = s.split(";");
        StoreSystem storeSystem = new StoreSystem();
        for (String s1 : split) {
            String commandS = s1.substring(0,1);
            String command = "";
            if (s1.length() > 2) {
                command = s1.substring(2);
            }
            switch (commandS) {
                case "r":
                    storeSystem.init(command);
                    break;
                case "p":
                    storeSystem.p(Integer.parseInt(command));
                    break;
                case "q":
                    storeSystem.q(command);
                    break;
                case "b":
                    storeSystem.b(command);
                    break;
                case "c":
                    storeSystem.c();
                    break;
                default:
                    break;
            }
        }
    }
}
